import axios from 'axios'
/**
 * 抽象接口请求方法
 * @returns {*}
 */
window.requestList = []
export function createAPI() {

  axios.defaults.withCredentials = true
  axios.defaults.timeout = 60000
  axios.defaults.headers['deviceInfo'] = 'web'
  axios.defaults.headers['responseType'] = 'blob'
  axios.defaults.headers['Content-Type'] = 'application/json;charset=UTF-8'
  let CancelToken = axios.CancelToken
  // axios.defaults.baseURL = 'http://195.195.8.83:7001/cyjjyq/'
  // axios.defaults.baseURL = 'http://195.195.8.84:7001/cyjjyq/'
  // axios.defaults.baseURL = 'http://5h954m.natappfree.cc'
  axios.defaults.baseURL = 'http://wangyuxin.natapp1.cc'
  // axios.defaults.baseURL = 'http://8jz3rs.natappfree.cc'
  axios.interceptors.request.use(res => {
    let config = res
    const token = window.sessionStorage.getItem('token');
    token ? config.headers.token = token : null;
    config.headers['Content-Type'] = 'application/json;charset=UTF-8'
    return config
  }, err => {
    console.log('err')
    return Promise.reject(err)
  })

  axios.interceptors.response.use(res => {
    if (res.status >= 200 && res.status < 400) {
      return Promise.resolve(res)
    }

    return Promise.reject(res)
  }, err => {
    if (axios.isCancel(err)) {
      // 取消请求
      return Promise.reject(err)
    } else {
      // 处理错误
      // this.$message({
      //   title: '系统异常',
      //   desc: '请稍后尝试'
      // })
      return Promise.reject(err)
    }
  })

  let api = {
    /**
     * get请求
     * @param target  请求地址
     * @param params  请求参数
     * @returns {Promise}
     */
    get (target, params = {}) {
      let suffix = Object.keys(params).map(name => {
        return `${name}=${params[name]}`
      }).join('&')

      let urls = suffix.length > 0 ? `${target}?${suffix}` : `${target}`
      return new Promise((resolve, reject) => {
        axios.get(urls, params, {headers: {}}).then(res => {
          resolve(res)
        }).catch(thrown => {
          reject(thrown)
        })
      })
    },
    /**
     * post请求
     * @param target  请求地址
     * @param params  请求参数
     * @returns {Promise}
     */
    post(target, params = {}) {
      let suffix = Object.keys(params).map(name => {
        return `${name}=${params[name]}`
      }).join('&')
      let urls = suffix.length > 0 ? `${target}?${suffix}` : `${target}`
      return new Promise((resolve, reject) => {
        axios.post(urls).then(res => {
          resolve(res)
        }).catch(thrown => {
          // 处理错误
          if (!axios.isCancel(thrown)) {
            // 处理错误
            reject(thrown)
          }
        })
      })
    },
    /**
     * form表单方式提交
     * @param target
     * @param params
     * @returns {Promise<any>}
     */
    postJSON(target, params = {}) {
      return new Promise((resolve, reject) => {
        axios.post(target, params, {
          headers: {}
        }).then(res => {
          resolve(res)
        }).catch((err) => {
          reject(err)
        })
      })
    },
  }
  return api
}
